package cn.itcast.service.system.impl;


import cn.itcast.dao.system.ModuleDao;
import cn.itcast.dao.system.UserDao;

import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.User;

import cn.itcast.service.system.UserService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private ModuleDao moduleDao;

    //@Autowired
    //private AmqpTemplate amqpTemplate;

    @Autowired
    private JavaMailSender javaMailSender;

    public void setJavaMailSender(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }

    @Override
    public List<User> findAll(String CompanyId) {
        return userDao.findAll(CompanyId);
    }

    @Override
    public void save(User user) {
        String pass = user.getPassword();
        //对密码进行加密( new Md5Hash   加盐加密)
        String password = user.getPassword();
        password = new Md5Hash(password,user.getEmail(),2).toString();

        user.setPassword(password);
        userDao.save(user);

//        发送一个邮件  RabbitMQ  通知用户
     /*   Map<String,String> emailMap = new HashMap<>();

        emailMap.put("to",user.getEmail());//给登录人邮箱发送邮件
        emailMap.put("subject","欢迎成为本网站会员");//设置主题
        emailMap.put("context","你的登录密码是"+pass);//设置正文内容

        amqpTemplate.convertAndSend("mail.send",emailMap);*/

//     使用spring容器中的发送邮件的方法
        //创建邮件对象

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");

            helper.setFrom("shiyu_2114@sina.com");
            helper.setTo(user.getEmail());
            helper.setSubject("欢迎成为本网站的会员");

            //添加附件
            helper.addAttachment("1.jpg",new File("D:\\用户目录\\我的图片\\照片\\1.jpg\\"));
            //添加正文和图片
            helper.setText("您的登录密码是："+pass+"<img src='cid:cid'/>",true);
            helper.addInline("cid",new File("D:\\用户目录\\我的图片\\照片\\1.jpg\\"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        javaMailSender.send(mimeMessage);


    }

    @Override
    public User findById(String id) {
        return userDao.findById(id);
    }

    @Override
    public void update(User user) {
        userDao.update(user);
    }

    @Override
    public void deleteById(String id) {


        userDao.deleteById(id);
    }

    @Override
    public PageInfo<User> findByPage(String CompanyId,Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userDao.findAll(CompanyId);

        return new PageInfo<>(list);

    }

    @Override
    public void changeRole(String userid, String[] roleIds) {

        //删除之前用户的角色数据 delete from pe_role_user where user_id=?
        userDao.deleteRoleAndUserByUserId(userid);

        //向角色中间表中插入数据
        for (String roleid : roleIds) {
            userDao.saveUserAndRole(userid,roleid);
        }
    }

    @Override
    public void deleteUserAndRole(String id) {
        userDao.deleteUserAndRole(id);
    }

    @Override
    public User findByEmail(String email) {
        return userDao.findByEmail(email);
    }



}
